﻿using System.Collections.Generic;
using System.Xml;
namespace ZhhbSoft.Data.Criterion
{
    /// <summary>
    ///Node 的摘要说明
    /// </summary>
    public abstract class Node
    {
        protected string _tagName = "";
        protected string _text = "";
        protected INodeCollection _childs;
        protected Dictionary<string, string> _attributes = new Dictionary<string, string>();
        public string TagName
        {
            get { return _tagName; }
            set { _tagName = value; }
        }
        public string Text
        {
            get { return _text; }
            set { _text = value; }
        }
        /// <summary>
        /// 内部文本
        /// </summary>
        public string InnerText
        {
            get
            {
                return _text;
            }
            set
            {
                _text = value;
            }
        }

        public Dictionary<string, string> Attributes
        {
            get { return _attributes; }
            set { _attributes = value; }
        }
        public NodeCollection Childs
        {
            get { return (NodeCollection)_childs; }
            set { _childs = value; }
        }
        protected Node(string tagName)
        {
            _tagName = tagName;
        }
        public string InnerXML
        {
            get
            {
                string strXML = "<" + _tagName;
                foreach (KeyValuePair<string, string> keyValuePair in _attributes)
                {
                    strXML += " " + keyValuePair.Key + "=\"" + keyValuePair.Value + "\"";
                }
                strXML += ">";
                if (_childs != null)
                {
                    foreach (Node node in _childs)
                    {
                        strXML += node.InnerXML + "\r\n";
                    }
                }

                strXML += _text;

                strXML += "</" + _tagName + ">";
                return strXML;
            }
            set
            {
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.LoadXml(value);
                FillFromXML(xmlDocument.SelectSingleNode("."));
            }
        }

        public abstract void FillFromXML(XmlNode xmlNode);
    }
}